#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
@author coldwind
"""
import tensorflow as tf

"""
    tf.name_scope() 返回的是 一个string
    tf.variable_scope() 返回的是一个 VariableScope 对象
    
    name_scope是给op_name加前缀, 
    variable_scope是给get_variable()创建的变量的名字加前缀。
    
    共享变量的前提是，变量的名字是一样的，变量的名字是由变量名和其scope前缀一起构成!
"""


# 测试一
# with tf.name_scope("hello"):
#     w = tf.get_variable('W', shape=[5, 2], initializer=tf.random_normal_initializer(mean=0, stddev=1.0),
#                         dtype=tf.float32)
#     b = tf.Variable(name='b', initial_value=tf.constant(0.1, shape=[2]), dtype=tf.float32)
#     # W:0
#     print(w.name)
#     # hello/b:0
#     print(b.name)
#
# # 无论放在name_scope里面还是外面都报错(Variable W already exists)
# w1 = tf.get_variable('W', shape=[5, 2], dtype=tf.float32)

# 测试二
# with tf.variable_scope("hello", reuse=tf.AUTO_REUSE) as scope:
#     w = tf.get_variable('W', shape=[5, 2], initializer=tf.random_normal_initializer(mean=0, stddev=1.0),
#                         dtype=tf.float32)
#     b = tf.Variable(name='b', initial_value=tf.constant(0.1, shape=[2]), dtype=tf.float32)
#     # hello/W:0
#     print(w.name)
#     # hello/b:0
#     print(b.name)
#
#     # 共享变量W(这里w1==w), 如果将w1放在scope外面, 则报错(Variable W already exists)
#     w1 = tf.get_variable('W', shape=[5, 2], dtype=tf.float32)
#     assert w1 == w

# 测试三
# 结论: variable_scope 和 name_scope都会给op的name加上前缀
#       是因为创建variable_scope时内部会创建一个同名的name_scope
# with tf.name_scope('hello'):
#     with tf.variable_scope('world'):
#         w = tf.get_variable('w', shape=[2])
#         res = tf.add(w, [3])
#
#         # world/w:0
#         print(w.name)
#         # hello/world/Add:0
#         print(res.name)
